package org.jeecg.modules.wlhy.utils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class PhotoCrawlerUtil {

    public static void main(String[] args) {
    }


    /**
     * 下载绝对路径图片
     * @param downloadUrl http://utrailer-hz.oss-cn-hangzhou.aliyuncs.com/ent-img/63fbcd6c-16b8-4b78-8763-140d589e6e31.jpg
     * @param serverPath  data/upFiles
     * @param savefolderName  青显武 或者 京A12345
     * @return
     */
    public static String downloadAbsolutePathPic(String downloadUrl, String serverPath, String savefolderName) {

        if (StrUtil.isEmpty(downloadUrl)) {
            return StrUtil.EMPTY;
        }

        //服务器文件夹
        String savefolderPath = serverPath + File.separator + savefolderName;
        if (!FileUtil.exist(savefolderPath)) {
            FileUtil.mkdir(savefolderPath);
        }

        //取文件名xxx.jpg
        int lastIndex = downloadUrl.lastIndexOf('/');
        String filename = downloadUrl.substring(lastIndex);

        //返回图片相对路径
        String saveRelativeFilePath = savefolderName + filename;

        File file = new File(savefolderPath + filename);
        if (file.exists()) {
            return saveRelativeFilePath;
        }

        long result = HttpUtil.downloadFile(downloadUrl, savefolderPath + filename);
        log.info("download file result : {} -> {} -> {}", downloadUrl, savefolderPath + filename ,result);

        return saveRelativeFilePath;
    }


}
